<?php

namespace App\Service\Work;

use App\Models\Works\ArticleModel;
use DB;

class WorksRelateService
{
    public static function getRandWorks($uid, $page, $offset)
    {
        $data =  DB::select("select
(select count(*) from lsj_praise_log b where b.articleid=a.id)  as praise_num,
(select count(*) from lsj_comment_log b where a.id=b.worksid) as comment_num,
(select count(*) from lsj_order b where a.id=b.article_id and b.paystatus=1) as order_num,
(select count(*) from lsj_praise_log b where b.articleid=a.id and b.uid= ?) as is_praise,
(select count(*) from lsj_order b where a.id=b.article_id and b.paystatus=1 and b.uid=?) as is_buy,
id,title,introduction,author,price,thumb,create_time,author_uid,reading
from lsj_article a
where a.status=1
and a.is_recommend=1 and a.craw_class=0 and a.is_onsale=1 and a.id!=25819
order by a.id desc
limit ?,?",[$uid,$uid,$offset,$page]);

        return $data;
    }
	
	/**
     * 根据id获得作品
     * @param $uid
     * @param $work_id
     * @return mixed
     */
    public static function getUserWork($work_id)
    {
        $result = DB::table('lsj_article')
            ->where('id',$work_id)
            ->first();
        return $result;
    }

    /**
     * 用户是否已举报
     */
    public static function isUserInfo($uid, $work_id)
    {
        $result = DB::table('lsj_article_info')
			->where(['uid'=>$uid,'id'=>$work_id])
            ->get();
        return $result;
    }

    /**
     * 举报作品
     */
    public static function addInfWorks(array $data)
    {
        $result = DB::table('lsj_article_info')
            ->insert($data);
        return $result;
    }

    /**
     * 上架或者下架作品
     */
    public static function changeWorksSale($article_id, $is_sale)
    {
        $result = DB::table('lsj_article')->where('id',$article_id)->update(['is_onsale'=>$is_sale]);
        return $result;
    }

    /**
     * add商品购买数量
     */
    public static function addWorksBuyNum($article_id)
    {
        $result = DB::table('lsj_article')->where('id',$article_id)->increment('buy_num',1);
        return $result;
    }

    /**
     * 获得广告
     */
    public static function getAds()
    {
        $result = DB::table('lsj_ads_banner')->get();
        return $result;
    }

    /**
     * 获得通知文章
     */
    public static function getNotice($page = 5)
    {
        $result = DB::table('lsj_ads_notice')->select('id','message')->limit($page)->get();
        return $result;
    }
}